<?php
namespace common\tool;

use yii;
use yii\db\Query;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;

/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/08/20
 */
trait Curd {
    /**
     * 获取一个标量
     * @param string|array|Expression $condition
     * @param array string|array|Expression $field
     * 所需字段 either a string (e.g. "id, name") or an array (e.g. ['id', 'name']) or an DB [[Expression]] object
     * @param array $params 查询绑定参数
     * @return string|null|false
     */
    public static function getScalar($condition, $field = ['id'], $params = []) {
        return (new Query())->select($field)->from(static::tableName())->where($condition, $params)->scalar();
    }

    /**
     * 获取一条记录
     * @param string|array|Expression $condition
     * @param array string|array|Expression $field
     * 所需字段 either a string (e.g. "id, name") or an array (e.g. ['id', 'name']) or an DB [[Expression]] object
     * @param array $params 查询绑定参数
     * @return array | false
     */
    public static function getOne($condition, $field = ['*'], $params = []) {
        return (new Query())->select($field)->from(static::tableName())->where($condition, $params)->one();
    }

    /**
     * 获取多条记录
     * @param $condition
     * @param array $field
     * @param string $orderBy
     * @param int $offset
     * @param int $limit
     * @param array $params
     * @return array
     */
    public static function getList($condition = [], $field = ['*'], $orderBy = '', $offset = null, $limit = null, $params = []) {
        $where = (new Query())->select($field)->from(static::tableName())->where($condition, $params);
        if ($orderBy) $where->orderBy($orderBy);
        if ($offset !== null) $where->offset($offset);
        if ($limit !== null) $where->limit($limit);
        return $where->all();
    }

    public static function getColumn($condition, $field = ['*'], $orderBy = '', $offset = null, $limit = null, $params = []) {
        $where = (new Query())->select($field)->from(static::tableName())->where($condition, $params);
        if ($orderBy) $where->orderBy($orderBy);
        if ($offset !== null) $where->offset($offset);
        if ($limit !== null) $where->limit($limit);
        return $where->column();
    }

    /**
     * @param $condition
     * @param array $params
     * @return int 影响行数
     */
    public static function delete($condition, $params = []) {
        return static::getDb()->createCommand()->delete(static::tableName(), $condition, $params)->execute();
    }

    /**
     * @param $columns [ key=>value, ...]
     * @param string $condition
     * @param array $params
     * @return int 影响行数
     */
    public static function update($columns, $condition = '', $params = []) {
        return static::getDb()->createCommand()->update(static::tableName(), $columns, $condition, $params)->execute();
    }

    /**
     * @param $columns [ key=>value, ...]
     * @return mixed LastInsertID | false
     */
    public static function insert($columns) {
        $rst = static::getDb()->createCommand()->insert(static::tableName(), $columns)->execute();
        if ($rst == 0) {
            return false;
        }
        return static::getDb()->getLastInsertID();
    }

    /**
     * 声明要操作的数据库表
     * By default this method returns the class name as the table name by calling [[Inflector::camel2id()]]
     * with prefix [[Connection::tablePrefix]]. For example if [[Connection::tablePrefix]] is 'tbl_',
     * 'Customer' becomes 'tbl_customer', and 'OrderItem' becomes 'tbl_order_item'. You may override this method
     * if the table is not named after this convention.
     * @return string the table name
     */
    public static function tableName() {
        return '{{%' . Inflector::camel2id(StringHelper::basename(get_called_class()), '_') . '}}';
    }

    public static function getDb() {
        return Yii::$app->db;
    }
}